<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ include file="/wesite/view/common/base.jsp" %>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>用户名注册</title>
    <%@ include file="/wesite/view/common/common.jsp" %>
    <style type="text/css">
        html, body {
            background: #f2f2f2;
        }
        .loginbox.signbox{
            margin:0;
        }
    </style>
</head>
<body>
<div class="loginbox signbox">
    <div class="login-input">
        <div class="input-text"><span class="text-tit">用户名</span><input type="text" name="loginname" placeholder="请输入用户名"></div>
        <div class="input-text"><span class="text-tit">手机号</span><input type="number" name="phone" placeholder="请输入11位数手机号码"></div>
        <div class="input-text"><span class="text-tit">验证码</span><input class="code" id="smscode" name="smscode" type="number"
                                                               placeholder="请输入验证码">
            <span id="yzmbtn" onclick="tosendsms();" class="send">获取验证码</span></div>
        <div class="input-text"><span class="text-tit">密码</span><input type="password" name="pwd" placeholder="请输入密码"></div>

    </div>
    <input type="button" onclick="toregister()" value="注册">
    <div class="agreement">注册即同意<span onclick="window.location.href = ctx + '/we/article/agreement'">《用户协议》</span>
    </div>
    <div class="signbox-b"><span onclick="window.location.href = ctx + '/we/sign/register'">手机号注册</span></div>
</div>
</body>
<script type="text/javascript" src="${ctx }/website/js/sms.js"></script>
<script type="text/javascript" src="${ctx }/website/js/common.js"></script>
<script type="text/javascript" src="${webctx}/script/aui-toast.js"></script>
<script type="text/javascript">
    //校验密码
    function isPassword(str) {
        var isPassword = /^[a-zA-Z0-9_]{6,16}$/;
        var bl = isPassword.test(str);
        return bl;
    }


    //自定义账号注册
    function toregister() {
        var subing = false;
        var checked = true;
        if ($("[name='loginname']").val() == '' || $("[name='loginname']").val().length < 6 || $("[name='loginname']").val().length > 12) {
            new auiToast().fail({
                title: "用户名在6-12位之间",
                duration: 2000
            });
            checked = false;
        }

        if (!$w.isMobile($("[name='phone']").val())) {
            new auiToast().fail({
                title: "请输入手机号码",
                duration: 2000
            });
            checked = false;
        }

        if ($("#smscode").val() == "") {
            new auiToast().fail({
                title: "验证码不能为空",
                duration: 2000
            });
            checked = false;
        }

        if ($("[name='pwd']").val() == '' || $("[name='pwd']").val().length < 6 || $("[name='pwd']").val().length > 16) {
            new auiToast().fail({
                title: "密码在6-16位字符之间",
                duration: 2000
            });
            checked = false;
        } else if (!isPassword($("[name='pwd']").val())) {
            new auiToast().fail({
                title: "密码必须由字母数字下划线组成",
                duration: 2000
            });
            checked = false;
        }

        if (!subing && checked) {
            checkexistaccount(function () {
                subing = true;
                $w.httpRequest({
                    url: apiroot + "/we/user/register",
                    data: {
                        "loginname": $("[name='loginname']").val(),
                        "mobile": $("[name='phone']").val(),
                        "pwd": $("[name='pwd']").val(),
                        "smscode": $("[name='smscode']").val(),
                        "regtype": "account"
                    },
                    ok: function (ret) {
                        var user = ret.Data;
                        try {
                            sessionStorage.setItem("_newreg_", "true");
                            sessionStorage.removeItem("_curUrl_");
                            new auiToast().success({
                                title: "注册成功",
                                duration: 2000
                            });
                        } catch (e) {
                        }
                        window.location.href = user;
                    },
                    fail: function (ret) {
                        if (ret.MsgCode == '3') {
                            new auiToast().fail({
                                title: "短信验证码错误",
                                duration: 2000
                            });
                        } else {
                            new auiToast().fail({
                                title: "注册失败",
                                duration: 2000
                            });
                        }
                        return false;
                    },
                    all: function () {
                        subing = false;
                    }
                })
            })
        }
    }

    //校验账号是否存在
    function checkexistaccount(backfun) {
        $w.httpRequest({
            url: apiroot + "/user/checkAccount",
            method: "post",
            data: {"loginname": $("[name='loginname']").val()},
            ok: function (ret) {
                if (ret.Data.exist == 'true') {
                    new auiToast().fail({
                        title: "该用户名已被注册",
                        duration: 2000
                    });
                }
                else {
                    if (backfun) {
                        backfun();
                    }
                }
            }
        })
    }

    //发送验证码
    function tosendsms() {
        if (!$w.isMobile($("[name='phone']").val())) {
            new auiToast().fail({
                title: "请输入手机号码",
                duration: 2000
            });
        } else {
            sendsms($("[name='phone']").val(), 'reg', 'yzmbtn');
        }
    }

    $(window).resize(function(){
        var hig = parseInt($(".loginbox").outerHeight(true));//div的高度包括margin
        var hig2 = parseInt($(".signbox-b").height());        //底部内容块的高度
        var hig3 = parseInt($(".signbox-b").css("bottom"));
        var hig4 = hig + hig2 + hig3;
        var hig_h = document.documentElement.clientHeight ;     //屏幕可视高度

        if(hig4 > hig_h){
            $(".signbox-b").hide();
        }else{
            $(".signbox-b").show();
        }

    });



</script>
</html>